package com.zhuss.food.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.food.CreateFoodBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.FoodDao;
import com.zhuss.food.db.entity.Food;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.NoUtil;

/**
 * 创建餐品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("foodCreate")
public class FoodCreate extends AbstractProcessor<Object, CreateFoodBo> implements Processor<Object, CreateFoodBo> {

    @Autowired
    private FoodDao foodDao;
    
    @Autowired
	@Qualifier("foodRead")
	private FoodRead foodRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateFoodBo bo) throws WpsException {
    	String name = bo.getName();
    	Integer area = bo.getArea();
    	if (name == null || "".equals(name.trim())) {
    		throw new WpsException(ExceptionCode.餐品name为空);
    	}
    	
    	List<Food> list = foodRead.list(name, area);
    	if (!list.isEmpty()) {
    		throw new WpsException(ExceptionCode.餐品name已存在);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(CreateFoodBo param) {
    	Food record = new Food();
    	record.setFoodNo(NoUtil.FOOD + NoUtil.gen());
    	record.setCategory(param.getCategory());
    	record.setArea(param.getArea());
    	record.setName(param.getName());
    	record.setPrice(param.getPrice());
    	record.setStatus(param.getStatus());
    	record.setIntroduction(param.getIntroduction());
    	record.setPic(param.getPic());
    	record.setSort(param.getSort());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	foodDao.create(record);
    	param.setTargetId(record.getFoodId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateFoodBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.餐品创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
